package com.kuang.springcloud.controller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

//提供RestFul服务
@RestController
public class DeptController {
    @Resource
    private DeptService deptService;

    //获取一些配置的信息，得到具体的为服务
    @Resource
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{no}")
    public Dept queryDeptByNo(@PathVariable("no") Long no){
        return deptService.queryDeptByNo(no);
    }

    @GetMapping("/dept/list")
    public List<Dept> querryAll(){
        return deptService.querryAll();
    }

    @GetMapping("/dept/discover")
    //注册进来的服务，获取一些消息
    public Object discover(){
        //获得为服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discoveryServices:" + services);

        //得到一个具体的为服务信息,通过具体的id也就是applicationName
        List<ServiceInstance> instances = client.
                getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost() + "\t" +
                    instance.getUri() + "\t" +
                    instance.getPort() + "\t" +
                    instance.getServiceId() + "\t");
        }
        return this.client;
    }
}
